package com.hxht.cmp.controller;

import com.google.gson.JsonArray;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.Device;
import com.hxht.cmp.pojo.DevicePojo;
import com.hxht.cmp.service.DeviceService;
import com.hxht.cmp.utils.ExportUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 获取庭审主机状态
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("/getDevStatus")
    @ResponseBody
    public ResultResponse getDevStatus(String courtUid) {
        if (StringUtils.isBlank(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        JsonArray jsonArray = deviceService.getDevStatus(courtUid);
        return ObjectUtils.isEmpty(jsonArray) ? ResultResponse.failed() : ResultResponse.success(jsonArray);
    }

    /**
     * 到处法庭设备excel
     *
     * @param response
     * @param courtUid
     * @param fileTitle
     * @return
     */
    @RequestMapping("exportCourtEquipment")
    public String exportCourtEquipment(HttpServletResponse response, String courtUid, String courtroomName, String fileTitle) {
        List<DevicePojo> result = deviceService.selectDeviceByCourtUid(courtUid, courtroomName);
        try {
            String fileName = new String((fileTitle + "法庭设备").getBytes("gb2312"), "iso8859-1") + ".xlsx";
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setCharacterEncoding("utf-8");
            // response.setHeader("Content-disposition", "attachment; filename="
            // + "exdddcel" + ".xlsx");// 组装附件名称和格式
            String[] titles = {"编号", "法庭名称", "设备编号", "设备名称", "设备型号", "设备位置", "设备状态", "说明"};
            ServletOutputStream outputStream = response.getOutputStream();
            ExportUtils.ExportExcel(titles, result, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("countDeviceByCourtUid")
    public ResultResponse countDeviceByCourtUid(String courtUid, String courtroomName, int page, int pageSize) {
        if (ObjectUtils.isEmpty(courtUid) || ObjectUtils.isEmpty(page) || page < 1 || ObjectUtils.isEmpty(pageSize) || pageSize < 1) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(deviceService.countDeviceByCourtUid(courtUid, courtroomName, page, pageSize));
    }

    @RequestMapping("addDevice")
    public ResultResponse addDevice(Device device) {
        if (ObjectUtils.isEmpty(device)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(deviceService.addDevice(device));
    }

    @RequestMapping("updateDevice")
    public ResultResponse updateDevice(Device device) {
        if (ObjectUtils.isEmpty(device)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(deviceService.updateDevice(device));
    }

    @RequestMapping("selectDeviceByPrimaryKey")
    public ResultResponse selectDeviceByPrimaryKey(long id) {
        if (ObjectUtils.isEmpty(id)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(deviceService.selectDeviceByPrimaryKey(id));
    }

    @RequestMapping("deleteDeviceByPrimaryKey")
    public ResultResponse deleteDeviceByPrimaryKey(long id) {
        if (ObjectUtils.isEmpty(id)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(deviceService.deleteDeviceByPrimaryKey(id));
    }
}
